package com.example.project.controller;

import com.example.project.base.BaseApi;
import com.example.project.base.ResultBean;
import com.example.project.entity.TAdmin;
import com.example.project.entity.TPostType;
import com.example.project.entity.TSalary;
import com.example.project.service.TPostTypeService;
import com.example.project.service.TSalaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/tsalary")
@Api(value = "TSalaryController",tags = "薪资范围相关")
public class TSalaryController extends BaseApi {
    @Autowired
    TSalaryService tSalaryService;

    @RequestMapping(value = "/getAllTSalary",method = RequestMethod.POST)
    @ApiOperation("获取所有薪资范围")
    public ResultBean getAllTSalary(Integer pageNo,Integer pageSize){
        try{
            PageHelper.startPage(pageNo==null?0:pageNo,pageSize==null?0:pageSize);
            PageInfo<TSalary> info=new PageInfo<>(tSalaryService.queryAll());

            return ok(info);
        }catch (Exception e){
            e.printStackTrace();
            return fail("查询失败");
        }
    }

    @RequestMapping(value = "/addTSalary",method = RequestMethod.POST)
    @ApiOperation("添加薪资范围")
    public ResultBean addTSalary(@RequestParam String salaryRange){
        try{
            int maxSort=tSalaryService.selectMaxSort();
            TSalary tSalary=new TSalary(){{
                setSalaryRange(salaryRange);
                setSort(maxSort);
            }};
            tSalaryService.save(tSalary);
            return ok();
        }catch (Exception e){
            e.printStackTrace();
            return fail("查询失败");
        }
    }

    @RequestMapping(value = "/updateTSalary",method = RequestMethod.POST)
    @ApiOperation("修改薪资范围")
    public ResultBean updateTSalary(@RequestParam String salaryRange,@RequestParam Integer pkid){
        try{
            Integer salaryId=pkid;
            TSalary tSalary=new TSalary(){{
                setSalaryRange(salaryRange);
                setPkid(salaryId);
            }};
            tSalaryService.update(tSalary);
            return ok();
        }catch (Exception e){
            e.printStackTrace();
            return fail("查询失败");
        }
    }

    @RequestMapping(value = "/deleteByIds",method = RequestMethod.POST)
    @ApiOperation("删除范围")
    public ResultBean deleteByIds(@RequestParam(value = "pkids") List<Integer> pkids){
        if(pkids!=null&&pkids.size()>0){
            try{
                tSalaryService.deleteByIds(TSalary.class,pkids);
//                logger.debug("获取参数：：{}",pkids);
                return ok("成功");
            }catch (Exception e){
                e.printStackTrace();
                return fail("查询失败");
            }
        }else{
            return fail("未获取参数");
        }
    }
}
